//
//  MainController.swift
//  Weibo
//
//  Created by 刘蒙 on 16/1/10.
//  Copyright © 2016年 liumeng. All rights reserved.
//

import UIKit

class MainController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //系统的tabar是只读属性,不能直接修改 可以使用kvc进行间接修改
        let mainTabBar = MainTabBar()

        setValue(mainTabBar, forKey: "tabBar")
        
        mainTabBar.plusBtn.addTarget(self, action: "plusBtnDidClick", forControlEvents: .TouchUpInside)
        
        addChildViewControllers()
    }
    
    @objc private func plusBtnDidClick() {
        print(__FUNCTION__)
        print("我被点击了")
    }
    
    
    //抽取出来一个方法
    private func addChildViewControllers() {
        //这里调用addChildViewController方法
        addChildViewController(HomeController(), image: "tabbar_home", title: "首页", selectedImg: "tabbar_home_highlighted")
        addChildViewController(MessageController(), image: "tabbar_message_center", title: "消息", selectedImg: "tabbar_message_center_highlighted")
        addChildViewController(DiscoverController(), image: "tabbar_discover", title: "发现", selectedImg: "tabbar_discover_highlighted")
        addChildViewController(ProfileController(), image: "tabbar_profile", title: "我", selectedImg: "tabbar_profile_highlighted")
        
    }
    
    
    //添加一个子视图
    private func addChildViewController(vc: UIViewController, image: String, title: String, selectedImg: String) {
        
        //实例化控制器
//        let vc = HomeController()
        
        
        vc.tabBarItem.image = UIImage(named: image)?.imageWithRenderingMode(.AlwaysOriginal)
        
        vc.title = title
        
        vc.tabBarItem.selectedImage = UIImage(named: selectedImg)?.imageWithRenderingMode(.AlwaysOriginal)
        
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: .Selected)
        
        //设置导航控制器
        let nav = UINavigationController(rootViewController: vc)
        
        addChildViewController(nav)
    }

}







